package org.example.classloader;

import java.io.InputStream;
import java.net.URL;

/**
 * 自定义classLoader
 */
public class CustomClassLoader extends ClassLoader {

    private URL[] urls;

    public CustomClassLoader(URL[] urls) {
        super();
        this.urls = urls;
    }

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        String path = name.replace('.', '/') + ".class";
        for (URL url : urls) {
            try {
                InputStream in = url.openStream();
                byte[] b = new byte[in.available()];
                in.read(b);
                in.close();
                return defineClass(name, b, 0, b.length);
            } catch (Exception e) {
            }
        }
        throw new ClassNotFoundException("Class " + name + " not found");
    }
}
